package com.citymanage.demo.controller.ManagerController;

import com.citymanage.demo.po.worker.Worker;
import com.citymanage.demo.service.manager.*;
import com.citymanage.demo.service.person.PersonManageService;
import com.citymanage.demo.service.worker.WorkerManageService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RequestMapping("/manager")
@Controller
public class ManagerController {
    public Integer userCurrentID;
    @Autowired
    private SelectStall selectStall;
    @Autowired
    private DelStall delStall;
    @Autowired
    private SelectPerson selectPerson;
    @Autowired
    private DelPerson delPerson;
    @Autowired
    private AddStall addStall;
    @Autowired
    private WorkerManageService workerManageService;
    @Autowired
    private DelWorker delWorker;
    @Autowired
    private PersonManageService personManageService;
    @RequestMapping("/c_person" )
    public String showWorkerCPerson(Model model){
        model.addAttribute("users",selectPerson.findAll());
        return "manager/c_person";
    }
    @RequestMapping("/homePage")
    public String showhomePage(Model model){
        model.addAttribute("workers",workerManageService.workerScore());
        return "manager/homePage";
    }
    @RequestMapping("/c_worker")
    public String showWorkerCWorker(Model model){
        model.addAttribute("users",workerManageService.selectWorker());
       return "manager/c_worker";
    }
    @RequestMapping("/c_announce" )
    public String showWorkerCAnnounce(){
        return "manager/c_announce";
    }

    @RequestMapping("")
    public String showWorkerLogin( ) {

        return "manager/manager";

    }
    @RequestMapping("/c_place")
    public String selectStall(Model model){

        model.addAttribute("select",selectStall.selectAll() );
        return "manager/c_place";
    }

    @RequestMapping("/del")
    public  String del(@RequestParam("sid") int sid){

        System.out.println("there");
        delStall.delStall(sid);
        System.out.println("ssssshere");
        return "redirect:/manager/c_place";
    }

    @RequestMapping("/pdelete")
    public String pdelete(@RequestParam("ID") String id){
        Integer pid=Integer.valueOf(id);
        delPerson.delPerson(pid);
        return "redirect:/manager/c_person";
    }
    @RequestMapping("/wdelete")
     public String wdelete( @RequestParam("ID") String id){
        Integer wid=Integer.valueOf(id);
        delWorker.delWorker(wid);
        return "redirect:/manager/c_worker";
    }

    @RequestMapping(value = "/addStalls",method = RequestMethod.POST)
    public String addStalls(@RequestParam("slocation")String slocation,@RequestParam("simg")String simg){
        String absolute="/assets/img/";
        simg=absolute.concat(simg);
        addStall.addStall(slocation,simg);
        return "redirect:/manager/c_place";
    }
    @RequestMapping(value = "/updateWorker", method = RequestMethod.POST)
    public String updateWorker(@RequestParam("ID") String id,@RequestParam("account") String account, @RequestParam("phone") String phone, @RequestParam("password1") String password1, @RequestParam("password2") String password2, HttpSession session, Model model) {
        Integer wid=Integer.valueOf(id);
        if (password1.equals(password2)) {
            workerManageService.updateWorker(wid, account, phone, password1);
            return "redirect:/manager/c_worker";
        }
        else return "passwordError";
    }
    @RequestMapping(value = "/updatePerson", method = RequestMethod.POST)
    public String updatePerson(@RequestParam("ID") String id,@RequestParam("account") String account, @RequestParam("phone") String phone, @RequestParam("password1") String password1, @RequestParam("password2") String password2, HttpSession session, Model model) {
        Integer wid=Integer.valueOf(id);
        if (password1.equals(password2)) {
            personManageService.updatePerson(wid, account, phone, password1);
            return "redirect:/manager/c_person";
        }
        else return "passwordError";
    }

   @RequestMapping(value = "/addPerson" ,method = RequestMethod.POST)
    public String addPerson(@RequestParam("ID") String id,@RequestParam("account") String account, @RequestParam("phone") String phone, @RequestParam("password1") String password1){
        Integer key=Integer.valueOf(id);
        personManageService.addPerson(key,account,phone,password1);
       return "redirect:/manager/c_person";
   }

    @RequestMapping("/addWorker")
    public String addWorker(@RequestParam("ID") String id,@RequestParam("account") String account, @RequestParam("phone") String phone, @RequestParam("password1") String password1){
        Integer key=Integer.valueOf(id);
        workerManageService.addWorker(key,account,phone,password1);
        return "redirect:/manager/c_worker";
    }
}
